; CooBrowser安装脚本
#define MyAppName "CooBrowser"
#define MyAppVersion "1.0"
#define MyAppPublisher "CooBrowser Team"
#define MyAppURL "https://www.coobrowser.com/"
#define MyAppExeName "CooBrowser.exe"

[Setup]
AppId={{8A3F7D6E-1C5B-4A9F-8D7E-3F5A9C8D2E1B}}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppName}
DefaultGroupName={#MyAppName}
OutputDir=Output
OutputBaseFilename=CooBrowser_Setup
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "chinesesimplified"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "创建桌面图标"; GroupDescription: "附加图标:"; Flags: unchecked

[Files]
; 应用程序文件
Source: "bin\Release\net8.0-windows\win-x64\publish\CooBrowser.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "bin\Release\net8.0-windows\win-x64\publish\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs

; .NET Framework 8.0 安装程序将在安装过程中从网络下载

[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

[Run]
; 安装.NET Framework 8.0（如果需要）
Filename: "{tmp}\dotnet-installer.exe"; Parameters: "/quiet"; StatusMsg: "正在安装.NET Framework 8.0..."; Check: DotNetFrameworkNeeded
; 安装后运行应用程序
Filename: "{app}\{#MyAppExeName}"; Description: "启动 {#MyAppName}"; Flags: nowait postinstall skipifsilent

[Code]
// 检查.NET Framework 8.0是否已安装
function DotNetFrameworkNeeded(): Boolean;
var
  Success: Boolean;
  DotNetVersion: Cardinal;
  TestMode: Boolean;
begin
  // 测试模式开关 - 设置为True时强制显示下载提示
  TestMode := False; // 恢复正常检测模式
  
  if TestMode then
  begin
    Result := True;
    Exit;
  end;
  
  // 检查.NET Framework 8.0是否已安装
  Success := RegQueryDWordValue(HKLM, 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full', 'Release', DotNetVersion);
  if Success then
  begin
    // .NET Framework 4.8对应的Release值为528040
    // 这里我们假设如果有更高版本的.NET Framework，就不需要安装8.0
    // 实际使用时可能需要调整这个逻辑
    Result := DotNetVersion < 528040;
  end
  else
  begin
    // 如果无法查询注册表，假设需要安装
    Result := True;
  end;
end;

// 安装前初始化
function InitializeSetup(): Boolean;
var
  ResultCode: Integer;
begin
  Result := True;
  
  // 检查是否需要安装.NET Framework
  if DotNetFrameworkNeeded then
  begin
    // 提示用户需要安装.NET Framework
    if MsgBox('此应用程序需要.NET Framework 8.0才能运行。' + #13#10 + 
              '点击"是"打开浏览器下载.NET Framework 8.0，' + #13#10 + 
              '安装完成后请重新运行此安装程序。', mbInformation, MB_YESNO) = IDYES then
    begin
      ShellExec('open', 'https://builds.dotnet.microsoft.com/dotnet/Sdk/8.0.413/dotnet-sdk-8.0.413-win-x64.exe', '', '', SW_SHOW, ewNoWait, ResultCode);
      Result := False; // 中止安装
    end
    else
      Result := False; // 用户选择不下载，中止安装
  end;
end;
